package com.example.marketing;

import com.example.marketing.action.CompositeConditionsAndActions;
import com.example.marketing.business.Coupon;
import com.example.marketing.business.Order;
import com.example.marketing.business.User;
import com.example.marketing.business.UserLevel;
import com.example.marketing.market.MarketingEngine;
import com.example.marketing.market.MarketingRule;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;

public class MarketingExample {
    public static void main(String[] args) {
        // 创建营销框架
        MarketingEngine framework = new MarketingEngine();
        
        // 定义优惠券
        Coupon coupon = new Coupon(
            "C20250519", 
            "满200减50", 
            new BigDecimal("50"), 
            new Date(), 
            new Date(System.currentTimeMillis() + 30 * 24 * 60 * 60 * 1000)
        );
        
        // 添加规则：订单满100，且是会员用户，且参与过抽奖，赠送优惠券并加100积分
        framework.addRule(new MarketingRule(
            "RULE_20250519",
            "会员抽奖优惠",
            CompositeConditionsAndActions.amountGreaterThan(new BigDecimal("100"))
                .and(CompositeConditionsAndActions.userLevelAtLeast(UserLevel.SILVER))
                .and(CompositeConditionsAndActions.hasParticipatedLottery()),
            CompositeConditionsAndActions.compositeAction(Arrays.asList(
                CompositeConditionsAndActions.grantCouponAction(coupon),
                CompositeConditionsAndActions.addPointsAction(100)
            ))
        ));
        
        // 创建测试用户
        User goldUser = new User("U1001", "张三", UserLevel.GOLD, 500);
        
        // 创建符合条件的订单
        Order eligibleOrder = new Order(
            "ORD20250519001",
            new BigDecimal("150"),
            goldUser,
            true
        );
        
        // 处理订单
        System.out.println("处理符合条件的订单:");
        framework.processOrder(eligibleOrder).forEach(result -> {
            System.out.println("规则: " + result.getRuleName());
            System.out.println("结果: " + result.getMessage());
        });
        System.out.println("用户当前积分: " + goldUser.getPoints());
        
        // 创建不符合条件的订单（未参与抽奖）
        Order ineligibleOrder = new Order(
            "ORD20250519002",
            new BigDecimal("150"),
            goldUser,
            false
        );
        
        // 处理订单
        System.out.println("\n处理不符合条件的订单:");
        framework.processOrder(ineligibleOrder).forEach(result -> {
            System.out.println("规则: " + result.getRuleName());
            System.out.println("结果: " + result.getMessage());
        });
    }
}    